package com.ruoyi.littleRedBook.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.littleRedBook.domain.pojo.UserAddress;
import com.ruoyi.littleRedBook.mapper.UserAddressMapper;
import com.ruoyi.littleRedBook.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Override
    public AjaxResult addAddress(UserAddress address) {
        // 实际应用中应该从当前登录信息中获取用户ID
        Long currentUserId = 1L; // 实际应用中需要替换为真实的当前用户ID
        address.setUserId(currentUserId);

        // 如果设置为默认地址，先将该用户的其他地址设为非默认
        if (address.getIsDefault() != null && address.getIsDefault() == 1) {
            userAddressMapper.updateNonDefaultByUserId(currentUserId);
        } else {
            // 如果是首次添加地址，默认设为默认地址
            int count = userAddressMapper.countByUserId(currentUserId);
            if (count == 0) {
                address.setIsDefault(1);
            } else {
                address.setIsDefault(0);
            }
        }

        int rows = userAddressMapper.insert(address);
        if (rows > 0) {
            return AjaxResult.success("地址添加成功", address);
        } else {
            return AjaxResult.error("地址添加失败");
        }
    }

    @Override
    public AjaxResult updateAddress(UserAddress address) {
        // 实际应用中应该从当前登录信息中获取用户ID，并验证权限
        Long currentUserId = 1L; // 实际应用中需要替换为真实的当前用户ID

        // 检查地址是否存在且属于当前用户
        UserAddress existingAddress = userAddressMapper.selectById(address.getId());
        if (existingAddress == null || !existingAddress.getUserId().equals(currentUserId)) {
            return AjaxResult.error("地址不存在或无权限");
        }

        // 如果设置为默认地址，先将该用户的其他地址设为非默认
        if (address.getIsDefault() != null && address.getIsDefault() == 1) {
            userAddressMapper.updateNonDefaultByUserId(currentUserId);
        }

        address.setUserId(currentUserId); // 确保用户ID不会被篡改
        int rows = userAddressMapper.updateById(address);
        if (rows > 0) {
            return AjaxResult.success("地址更新成功");
        } else {
            return AjaxResult.error("地址更新失败");
        }
    }

    @Override
    public AjaxResult deleteAddress(Long id) {
        // 实际应用中应该从当前登录信息中获取用户ID，并验证权限
        Long currentUserId = 1L; // 实际应用中需要替换为真实的当前用户ID

        // 检查地址是否存在且属于当前用户
        UserAddress address = userAddressMapper.selectById(id);
        if (address == null || !address.getUserId().equals(currentUserId)) {
            return AjaxResult.error("地址不存在或无权限");
        }

        // 如果删除的是默认地址，需要将其他地址中的一个设为默认
        if (address.getIsDefault() == 1) {
            List<UserAddress> otherAddresses = userAddressMapper.selectByUserIdExcludeId(currentUserId, id);
            if (otherAddresses != null && !otherAddresses.isEmpty()) {
                UserAddress newDefault = otherAddresses.get(0);
                newDefault.setIsDefault(1);
                userAddressMapper.updateById(newDefault);
            }
        }

        int rows = userAddressMapper.deleteById(id);
        if (rows > 0) {
            return AjaxResult.success("地址删除成功");
        } else {
            return AjaxResult.error("地址删除失败");
        }
    }

    @Override
    public AjaxResult setDefaultAddress(Long id) {
        // 实际应用中应该从当前登录信息中获取用户ID，并验证权限
        Long currentUserId = 1L; // 实际应用中需要替换为真实的当前用户ID

        // 检查地址是否存在且属于当前用户
        UserAddress address = userAddressMapper.selectById(id);
        if (address == null || !address.getUserId().equals(currentUserId)) {
            return AjaxResult.error("地址不存在或无权限");
        }

        // 将该用户的其他地址设为非默认
        userAddressMapper.updateNonDefaultByUserId(currentUserId);

        // 设置当前地址为默认
        address.setIsDefault(1);
        int rows = userAddressMapper.updateById(address);
        if (rows > 0) {
            return AjaxResult.success("默认地址设置成功");
        } else {
            return AjaxResult.error("默认地址设置失败");
        }
    }

    @Override
    public AjaxResult getAllUserAddresses() {
        // 实际应用中应该从当前登录信息中获取用户ID
        Long currentUserId = 1L; // 实际应用中需要替换为真实的当前用户ID

        List<UserAddress> addresses = userAddressMapper.selectByUserId(currentUserId);
        return AjaxResult.success(addresses);
    }

    @Override
    public AjaxResult getDefaultAddress() {
        // 实际应用中应该从当前登录信息中获取用户ID
        Long currentUserId = 1L; // 实际应用中需要替换为真实的当前用户ID

        UserAddress defaultAddress = userAddressMapper.selectDefaultByUserId(currentUserId);
        return AjaxResult.success(defaultAddress);
    }

    @Override
    public AjaxResult getAddressById(Long id) {
        // 实际应用中应该从当前登录信息中获取用户ID，并验证权限
        Long currentUserId = 1L; // 实际应用中需要替换为真实的当前用户ID

        UserAddress address = userAddressMapper.selectById(id);
        if (address == null || !address.getUserId().equals(currentUserId)) {
            return AjaxResult.error("地址不存在或无权限");
        }

        return AjaxResult.success(address);
    }
}
